#pragma once
#include "cocos2d.h"
#include "tinyxml2/tinyxml2.h"
#include <vector>

#define CFGFILE "config.xml"
#define VOLUMEMASTER pConfig->getIntValue("audio","volmaster")/100.f
#define VOLUMEMUSIC pConfig->getIntValue("audio","volmusic")/100.f

#define CCKEYCODE EventKeyboard::KeyCode
#define KEYCODE UINT

USING_NS_CC;
using namespace std;
using namespace tinyxml2;
namespace tx = tinyxml2;

class Config
{
	public:
	Config();
	~Config();

	public:
	void createDefaultConfigFile();
	void loadConfig();
	void saveConfig();
	int getIntValue(char* cfgclass, char* cfgitem, char* attrname = "value");
	float getFloatValue(char* cfgclass, char* cfgitem, char* attrname = "value");
	const char* getCfgValue(char* cfgclass, char* cfgitem, char* attrname = "value");
	void setIntValue(char* cfgclass, char* cfgitem, int attrval, char* attrname = "value", bool autosave = true);
	void setFloatValue(char* cfgclass, char* cfgitem, float attrval, char* attrname = "value", bool autosave = true);
	void setCfgValue(char * cfgclass, char * cfgitem, char * attrval, char * attrname, bool autosave = true);

	private:
	tx::XMLDocument* xdoc;
	struct CFGITEM
	{
		char* itemname;
		char* defaultvalue;
	};
	struct CFGCLASS
	{
		char* classname;
		vector<CFGITEM> items;
	};
	vector<CFGCLASS> configScheme;

	void setConfigScheme();
	vector<CFGCLASS>::iterator addClassToConfigScheme(char* classname);
	void addClassItemToConfigScheme(vector<CFGCLASS>::iterator cfgclass, char* itemname, char* defvalue);
	void addClassItemToConfigScheme(vector<CFGCLASS>::iterator cfgclass, char* itemname, string defvalue);
	void checkConfigScheme();
	tx::XMLElement* addElement(tx::XMLNode* target, char* name, char* attrval = NULL, char* attrname = "value");
	tx::XMLElement* getElement(char* cfgclass, char* cfgitem);
};

class ConfigKeys
{
	public:
	ConfigKeys();
	virtual ~ConfigKeys();

	public:
	KEYCODE key4[4];
	KEYCODE key5[5];
	KEYCODE key6[6];
	KEYCODE key7[7];
	KEYCODE key8[8];

	KEYCODE key4b[4];
	KEYCODE key5b[5];
	KEYCODE key6b[6];
	KEYCODE key7b[7];
	KEYCODE key8b[8];

	KEYCODE** keySets;
	KEYCODE** keySetsB;
	int activeKeySetIndex = 0;
	int activeKeySetType = 1; //1=normal, 2=bm style;

	public:
	void setActiveKeySet(int trackCount);
	void setActiveType(int type);
	KEYCODE* getKeyset(int keyCount, int type);
	KEYCODE* getActiveKeySet();
	KEYCODE getTrackBindingKey(int trackCount);

	private:
	void initKeySets();
	KEYCODE* getKeycodeA(int keycount);
	KEYCODE* getKeycodeB(int keycount);
};